package com.ocean.search.listener;

import com.ocean.search.util.SearchUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 启动完成后注册需要使用的es索引
 * 也可以使用SpringBoot的集成功能
 *
 * @author zhang
 * @since 15:29 2021/6/28
 */
@Slf4j
@Component
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

    /**
     * 商品检索索引
     */
    private static final String PROPOSAL_INDEX = "search-index";

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        try {
            SearchUtil.createIndex(PROPOSAL_INDEX, 1, 0, "search/ApplicationEsModel.json");
        } catch (Exception e) {
            log.error("连接ElasticSearch失败,ES服务将无法使用,原因:{}", e.getMessage());
        }
    }
}